package ch.zbw.pra.sysventory.domain.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	public Calendar getTodayDateAtMidnight() {
		Date today = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(today);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar;
	}

	public Calendar getTodayDateBeforeMidnight() {
		Date today = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(today);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		return calendar;
	}

	public Date getFirstDateOfActualMonth() {
		Calendar calendar = this.getTodayDateAtMidnight();
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTime();
	}

	public Date getLastDateOfActualMonth() {
		Calendar calendar = this.getTodayDateBeforeMidnight();
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return calendar.getTime();
	}

	public Date getFirstDateOfActualWeek() {
		Calendar calendar = this.getTodayDateAtMidnight();
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return calendar.getTime();
	}

	public Date getLastDateOfActualWeek() {
		Calendar calendar = this.getTodayDateBeforeMidnight();
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		return calendar.getTime();
	}

	public Date getFirstDateOfActualYear() {
		Calendar calendar = this.getTodayDateAtMidnight();
		calendar.set(Calendar.DAY_OF_YEAR, 1);
		return calendar.getTime();
	}

	public Date getLastDateOfActualYear() {
		Calendar calendar = this.getTodayDateBeforeMidnight();
		calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
		return calendar.getTime();
	}

	public String getFormattedDate(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("dd.MM.yy");
		return format.format(date);
	}

}
